// pages/owner/owner.js
const request = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname:'',
    sign:'',
    gender:'',
    idcard:'',
    // index: null,
    picker: ['男', '女'],
    id:'',
    password:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init()
  },

  bsign(e){
    this.setData({
      sign:e.detail.value
    })
  },

  PickerChange(e) {
    console.log(e);
    this.setData({
      gender: Number.parseInt(e.detail.value)
    })
    console.log(this.data.gender)
  },

  bnickname(e){
    this.setData({
      nickname:e.detail.value
    })
  },
  bgender(e){
    this.setData({
      gender:e.detail.value
    })
  },
  bidcard(e){
    this.setData({
      idcard:e.detail.value
    })
  },

  bpassword(e){
    this.setData({
      password:e.detail.value
    })
  },
  init(){
    request.sendRequest("/owner/ownerInfo","GET",{
      ownerId:getApp().globalData.userInfo.id
    }).then((res)=>{
      this.setData({
        idcard:res.data.owner.idCard,
        sign:res.data.owner.sign,
        nickname:res.data.owner.nickName,
        gender: Number.parseInt(res.data.owner.gender) -1,
        id:res.data.owner.id,
      })
      console.log(this.data.nickname)
    }).catch((err)=>{
      console.error(err)
    })

  },

  SubmitImages(){
    let _IDRe18 = /^\d{6}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[\dX]$/
    const passwordReg = /^.{6,}$/

    if( !_IDRe18.test( this.data.idcard )) {
      wx.showToast({
        title: '身份证格式错误',
        icon:'error'
      })
      return;
    } 

    if(this.data.password!=null && this.data.password!=''){
      if(!passwordReg.test(this.data.password)){
        wx.showToast({
          title: '密码不能少于6',
          icon:'error'
        })
        return;
      }
    }


    request.sendRequest("/owner/update","POST",{
      idcard:this.data.idcard,
        sign:this.data.sign,
        nickname:this.data.nickname,
        gender: Number.parseInt(this.data.gender) +1,
        ownerId:this.data.id,
        password:this.data.password
    }).then((res)=>{
      console.log(res)
      wx.showToast({
        title: '修改成功',
      })
      this.init()
    }).catch((err)=>{
      console.error(err)
    })
  },

  change(){
    wx.redirectTo({
      url: '../editOwner/editOwner',
    })

  },

  submit(){
    getApp().globalData.userInfo = null
    wx.removeStorageSync('token')
    wx.redirectTo({
      url: '../index/index',
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})